<template>
	<view class="login">
		<view class="login-body">
			<view class="input-item">
				<view class="input-name">手机号码</view>
				<u--input placeholder="请输入手机号码" v-model="loginForm.mobile" border="surround"></u--input>
			</view>
			<view class="input-item">
				<view class="input-name">验证码</view>
				<u--input placeholder="请输入验证码" v-model="loginForm.vcode"></u--input>
				<text class="codeBtn" @click="handleVerifyphone">获取验证码</text>
			</view>
			<view class="input-item">
				<view class="input-name">重置密码</view>
				<u--input placeholder="请输入密码" type="password" v-model="loginForm.password" border="surround"></u--input>
			</view>
			<view class="input-item">
				<view class="input-name">确认密码</view>
				<u--input placeholder="请再次输入密码" type="password" v-model="loginForm.passwords"
					border="surround"></u--input>
			</view>
			<view class="login-btn">
				<u-button @click="handleRegister" type="primary">确认</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getCode,
		forgotPassword,
		verifyphone
	} from '@/api/login'

	export default {
		data() {
			return {
				tips: '获取验证码',
				loginForm: {
					vcode: "",
					password: "",
					passwords: "",
					mobile: "",
					deviceId: this.$store.state.user.deviceId,
				},
			}
		},
		methods: {
			handleVerifyphone() {
				// verifyphone(this.loginForm.mobile).then(res => {
				// 	uni.$u.toast(res.result);
				// 	if (res.status == 0) {
				// 		this.getCode();
				// 	}
				// })
				uni.$u.toast("获取成功");
			},
			handleRegister() {
				if (this.loginForm.password !== this.loginForm.passwords) {
					uni.$u.toast('密码不一致');
					return;
				}
				uni.showLoading({
					title: '正在提交信息'
				})
				forgotPassword(this.loginForm).then(res => {
					if (res.status == 0) {
						uni.hideLoading();
						uni.$u.toast('重置成功');
						setTimeout(() => {
							this.$tab.reLaunch('/pages/login')
						}, 2000);
					}
				})
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					getCode(this.loginForm.mobile, 1).then(res => {
						if (res.status == 0) {
							uni.hideLoading();
							uni.$u.toast('验证码已发送');
							this.$refs.uCode.start();
						}
					})
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;

		.login {
			background: #6699ff;
			height: 100vh;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			.login-head {
				padding-top: 5%;

				.title {
					font-size: 30px;
					color: #fff;
					margin-bottom: 5%;
					text-align: center;
				}
			}

			.login-body {
				background: #fff;
				border-radius: 10px;
				width: 80vw;
				margin: 15px auto;
				padding: 10px 15px 0;

				.codeBtn {
					background: #3c96f3;
					padding: 9px 3px;
					color: #fff;
				}

				.input-item {
					padding: 10px 0;
					display: flex;
					align-items: center;

					.input-name {
						width: 17vw;
						font-size: 16px;
						color: #666;
						margin-bottom: 5px;
					}

					.input {
						font-size: 18px;
						color: #7f8a96;
					}
				}

				.password-option {
					margin-top: 15px;
					display: flex;
					justify-content: space-between;

					.input-text {
						font-size: 14px;
						color: #666;
					}
				}

				.login-btn {
					margin-top: 20px;
					padding-bottom: 5px;

					.u-button {
						margin-bottom: 15px;
					}
				}
			}
		}
	}
</style>